#-*- coding: utf8 -*-
from tutor.scripts import *
from tutor.plugins.sympy import *

bignum = 50
n_iter = 0
def rand():
    return One * oneof(2, 3, 4, 6, 9, 12, 18) / 6

def test_OK(n):
    a, b = (n / pi ** 2).as_numer_denom()
    if a < bignum and b < bignum:
        return True
    else:
        return False

iter = 0
while True:
    iter += 1

    with display_block('Entradas'):
        a = rand()
        ra = rand()
        rb = rand()
        Nf = oneof(One / 6, One / 4, Half, One, 3 * Half)
        N0 = oneof(One / 6, One / 4, Half, One, 3 * Half)
        N0, Nf = min(N0, Nf), max(N0, Nf)
        raT = ra * t
        rbT = rb * t
        T0 = pi * N0
        Tf = pi * Nf
        ya = y * a
        x0, y0 = ra * T0 * cos(T0) ** 2, rb * T0 * sin(T0) ** 2
        xf, yf = ra * Tf * cos(Tf) ** 2, rb * Tf * sin(Tf) ** 2
        phi = x * y + a * y ** 2 / 2
        phi0 = simplify(phi.subs([(x, x0), (y, y0)]))
        phif = simplify(phi.subs([(x, xf), (y, yf)]))
        iter += 1

    with display_block('Resultados, comprimento'):
        work = phif - phi0
        pontoFinal = phif
        potxy = work - a / 2 * (yf ** 2 - y0 ** 2)

    # Distratores
    resps = set([work, pontoFinal, potxy])
    if (len(resps) < 3) or (Zero in resps):
        continue
        
    distr = set([])
    while len(distr) < 4:
        aux = oneof(*resps)
        aux *= rand()
        if aux not in resps:
            distr.add(aux)

    with display_block('Distratores'):
        dis1, dis2, dis3, dis4 = distr

    # Test complexity of numbers
    if all(test_OK(x) for x in resps):
        break

print 'Número de iterações: ', iter
